<cfcomponent output="false" extends="cfhibernate.model.baseHibernateDAO" displayname="userDAO" hint="I am a user data access object.">

	<cffunction name="init" access="public" returntype="any" output="false">
		<cfargument name="dsn" type="string" required="true">
		<cfset variables.dsn = arguments.dsn>
		<cfreturn this>
	</cffunction>

	<cffunction name="save" access="public" returntype="void" output="false">
		<cfargument name="user" type="cfhibernate.model.user.user" required="yes">
		<cfset var user2 = mapToHashMap(arguments.user)>
		<cfset var addresses = user2["addresses"]>
		<cfset var iterator = addresses.iterator()>
		<cfset var element = "">
		<cfset var i = 0>
		<cfset var sessionFactory = Application.hibernateConfig.buildSessionFactory()>
		<cfset var EntityMode = createObject("java", "org.hibernate.EntityMode")>
		<cfset var mySession = sessionFactory.openSession()>
		<cfset var tx = 0>
		<!--- <cfdump var="#user2.toString()#" label="mapToHashMap(arguments.user)"><cfabort> --->
		<cfset mySession = mySession.getSession(EntityMode.MAP)>
		<cfset tx = mySession.beginTransaction()>
		<!--- <cfdump var="#user2.toString()#" label="user2"><cfabort> --->
		<!--- <cfloop condition="#iterator.hasNext()#">
			<cfset element = iterator.next()>
			<cfset mySession.saveOrUpdate(element)>
		</cfloop> --->
		<cfset mySession.saveOrUpdate(user2)>
		<cfset tx.commit()>
		<cfset mySession.close()>
		<cfset sessionFactory.close()>
		<!--- <cfset arguments.user = mapToCFC(user2)> --->
		
		<!--- <cfdump var="#user2["id"]#" label="user2['id']"><cfabort> --->
		<cfset arguments.user.setID(user2["id"])>
		<!--- <cfif arguments.user.getID() neq 0>
			<cfset update(arguments.user)>
		<cfelse>
			<cfset create(arguments.user)>
		</cfif> --->
	</cffunction>

	<cffunction name="read" access="public" returntype="cfhibernate.model.user.user" output="false">
		<cfargument name="userID" type="numeric" required="yes">
		<cfset var sessionFactory = Application.hibernateConfig.buildSessionFactory()>
		<cfset var mySession = sessionFactory.openSession()>
		<cfset var EntityMode = createObject("java", "org.hibernate.EntityMode")>
		<cfset var user = 0>
		<cfset mySession = mySession.getSession(EntityMode.MAP)>
		<cfset userData = mySession.createQuery("from User user where userID = #arguments.userID#").uniqueResult()>
		<!--- <cfdump var="#userData.toString()#" label="userData"><cfabort> --->
		<cfset user = mapToCFC(userData)>
		<!--- <cfset var data = structNew()>
		<cfset var user = 0>
		<cfset var readuser = 0>
		<cfquery name="readuser" datasource="#variables.dsn#">
		SELECT userid, first_name, last_name, email, middlename
		FROM users
		WHERE id = <cfqueryparam cfsqltype="cf_sql_integer" value="#userID#">
		</cfquery>
		<cfloop list="#readuser.columnList#" index="field">
			<cfset 'data.#field#' = evaluate('readuser.#field#')>
		</cfloop>
		<cfset user = createObject("component", "cfhibernate.model.user.user").init(argumentcollection=data)>
		<cfset user.setFirstName(readuser.first_name)>
		<cfset user.setLastName(readuser.last_name)> --->
		<cfset mySession.close()>
		<cfreturn user>
	</cffunction>

</cfcomponent>